package cn.tedu.myweb.mapper;

import cn.tedu.myweb.model.Permission;
import cn.tedu.myweb.model.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT p.id , p.name\n" +
            "            FROM user u\n" +
            "            LEFT JOIN user_permission up\n" +
            "            ON u.id=up.user_id\n" +
            "            LEFT JOIN permission p\n" +
            "            ON p.id=up.permission_id\n" +
            "            WHERE u.id=#{id};")
    List<Permission> findUserPermissionsById(int id);


    @Select("select * from user where username=#{username}")
    User findUserByUsername(String username);

    @Select("select * from user where phone=#{phone}")
    User findUserByPhone(String phone);

    @Select("select * from user where email=#{email}")
    User findUserByEmail(String email);

    @Select("SELECT r.id \n" +
            "            FROM user u\n" +
            "            LEFT JOIN room r\n" +
            "            ON u.id=r.user_id\n" +
            "            LEFT JOIN permission p\n" +
            "            ON p.id=up.permission_id\n" +
            "            WHERE u.id=#{id};")
    User findRoomByFrindId(String username);




    @Select("select * from user where uname=#{username}")
    User login(String username);

    @Select("select count(uid) from user where uname=#{uname}")
    int check(String uname);

    @Insert("insert into user values(null,null,#{uname},#{password},0,0,'新用户',null,null,null,null,#{regTime},null,0,0,0,0,null,null)")
    void reg(User user);

    @Select("select uid from user where uname=#{uname}")
    int selectUidByUname(String uname);

    @Update("update user set did=#{did} where id=#{id}")
    void updateDidByUid(int did, int id);

    @Insert("insert into depository values(null,#{id},0,10485760,null,null)")    //最大容量=10485760KB=10GB
    void createDepository(int id);

    @Select("select did from depository where id=#{uid}")
    User selectDidByUid(int id);

    @Select("select * from user where id=#{id}")
    User selectAllById(int id);
}
